package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.mapper.MicroSmartMoHeResidentMapper;
import com.smart.village.big.screen.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.Duration;
import java.util.List;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {
    @Value("${platform.appCode}")
    public String appCode;

    @Value("${platform.appPwd}")
    public String appPwd;

    @Value("${platform.tokenId}")
    public String tokenId;

    @Value("${platform.getTokenUrl}")
    public String getTokenUrl;

    @Value("${platform.getAuthUrl}")
    public String getAuthUrl;

    @Value("${platform.getTownVillageListTag}")
    public String getTownVillageListTag;

    @Value("${platform.getPartyOrganizationMicroMyIcon}")
    public String getPartyOrganizationMicroMyIcon;
    @Autowired
    public WebClient webClient;

    @Autowired
    private MicroSmartMoHeResidentMapper microSmartMoHeResidentMapper;

    @Override
    public String getToken() {
        return webClient.post() // 创建GET请求的构建器对象。 也可以使用 .post() 等方法创建其他类型的请求。
                .uri(getTokenUrl, urlBuilder -> urlBuilder.queryParam("appCode", appCode).queryParam("appPwd", appPwd).build())
                .header("tokenId", tokenId)

                .exchangeToMono(response -> response.bodyToMono(String.class))
                .timeout(Duration.ofSeconds(30))
                .block();
    }

    @Override
    public JSONObject getTownVillageList(String username) {
        String tokenIdFetch = webClient.post() // 创建GET请求的构建器对象。 也可以使用 .post() 等方法创建其他类型的请求。
                .uri(getTokenUrl, urlBuilder -> urlBuilder.queryParam("appCode", appCode).queryParam("appPwd", appPwd).build())
                .header("tokenId", tokenId)
                .exchangeToMono(response -> response.bodyToMono(String.class))
                .timeout(Duration.ofSeconds(30))
                .block();
        JSONObject tokenId1 = webClient.get() // 创建GET请求的构建器对象。 也可以使用 .post() 等方法创建其他类型的请求。
                .uri(getAuthUrl + "/" + username + "/" + getTownVillageListTag)
                .header("tokenId", tokenIdFetch)
                .header("content-type", "application/json")
                .header("accept", "application/json")
                .exchangeToMono(response -> response.bodyToMono(JSONObject.class))
                .timeout(Duration.ofSeconds(30))
                .block();
        return tokenId1;
    }

    @Override
    public JSONObject getPartyOrganizationMicroMyIcon(String username) {
        String tokenIdFetch = webClient.post() // 创建GET请求的构建器对象。 也可以使用 .post() 等方法创建其他类型的请求。
                .uri(getTokenUrl, urlBuilder -> urlBuilder.queryParam("appCode", appCode).queryParam("appPwd", appPwd).build())
                .header("tokenId", tokenId)
                .exchangeToMono(response -> response.bodyToMono(String.class))
                .timeout(Duration.ofSeconds(30))
                .block();
        JSONObject tokenId1 = webClient.get() // 创建GET请求的构建器对象。 也可以使用 .post() 等方法创建其他类型的请求。
                .uri(getAuthUrl + "/" + username + "/" + getPartyOrganizationMicroMyIcon)
                .header("tokenId", tokenIdFetch)
                .header("content-type", "application/json")
                .header("accept", "application/json")
                .exchangeToMono(response -> response.bodyToMono(JSONObject.class)) .timeout(Duration.ofSeconds(30))
                .block();
        return tokenId1;
    }

    @Override
    public List<JSONObject> getResidentChangeButtonPermissionList(String idCardNumber) {
        List<JSONObject> list = Lists.newArrayList();
        //{
        //        id: 1,
        //        name: "基层管理—网格员",
        //        url: "/villageResite/index/index",
        //      },
        //      {
        //        id: 2,
        //        name: "基层管理-巡边员",
        //      },

        int gridManagerPermission = microSmartMoHeResidentMapper.getGridManagerPermission(idCardNumber);
        if(gridManagerPermission > 0){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", 1);
            jsonObject.put("name", "基层管理—网格员");
            list.add(jsonObject);
        }

        int patrolManagerPermission = microSmartMoHeResidentMapper.getPatrolManagerPermission(idCardNumber);
        if(patrolManagerPermission > 0){
            JSONObject jsonObject = new JSONObject();
            jsonObject = new JSONObject();
            jsonObject.put("id", 2);
            jsonObject.put("name", "基层管理—巡边员");
            list.add(jsonObject);
        }
        return list;
    }
}
